/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.siglo21.ppo.persistence.dao;

import ar.com.siglo21.ppo.model.Autor;
import ar.com.siglo21.ppo.model.Genero;
import ar.com.siglo21.ppo.model.Libro;
import ar.com.siglo21.ppo.persistence.MySQLConnection;
import ar.com.siglo21.ppo.util.TrabajoIntegradorException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LibroDAOImpl implements LibroDAO {

    @Override
    public void agregar(Libro object) throws TrabajoIntegradorException, SQLException {
         String sql = "Insert into libro(titulo,id_autor,id_genero,editorial,isbn) values "
                + "('" + object.getTitulo()+  "','"+object.getAutor().getId()+"','"+object.getGenero().getId()+"','"+object.getEditorial()+"'"
                + ",'"+object.getIsbn()+"')";
        MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public boolean existe(Libro object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void eliminar(Libro object) throws TrabajoIntegradorException, SQLException {

        String sql = "DELETE FROM libro where id=" + object.getId();
        MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public Libro leer(int id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void actualizar(Libro object) throws TrabajoIntegradorException, SQLException {
       String sql="UPDATE LIBRO SET titulo='"+ object.getTitulo()+"',"+ "id_autor="+object.getAutor().getId()+", "+"id_genero="+object.getGenero().getId()+","+"editorial='"+ object.getEditorial()+"',"+"isbn='"+object.getIsbn()+"'" +" where id="+object.getId();
       MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public List<Libro> listarLibros() throws SQLException, TrabajoIntegradorException {
        String sql = "Select * from libro l, autor a, genero g where l.id_autor=a.id and l.id_genero=g.id";
        MySQLConnection.getDbCon().query(sql);
        Libro libro;
        Autor autor;
        Genero genero;

        List<Libro> listaLibros = new ArrayList();
        ResultSet resulset = MySQLConnection.getDbCon().query(sql);

        while (resulset.next()) {
            autor = new Autor();
            libro = new Libro();
            genero = new Genero();

            libro.setId(resulset.getInt(1));
            libro.setTitulo(resulset.getString(2));
            libro.setEditorial(resulset.getString(5));
            libro.setIsbn(resulset.getString(6));

            autor.setId(resulset.getInt(7));
            autor.setNombre(resulset.getString(8));
            autor.setApellido(resulset.getString(9));
            libro.setAutor(autor);

            genero.setId(resulset.getInt(10));
            genero.setDescripcion(resulset.getString(11));
            libro.setGenero(genero);

            listaLibros.add(libro);
        }

        return listaLibros;

    }

}
